package com.management.security;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.management.controller.info.AccountInfo;
import com.management.utils.CopyUtils;
import com.management.utils.ObjectRedisTemplate;
import com.management.utils.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class LoginSuccessHandler implements AuthenticationSuccessHandler {

    private ObjectMapper objectMapper=new ObjectMapper();
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf8");

        AccountInfo userInfo = CopyUtils.CopyOne(authentication.getPrincipal(), AccountInfo.class);



        Result result = Result.getInstance(AccountInfo.class).setCode(200).setMessage("登录成功").setData(userInfo);
        String s = objectMapper.writeValueAsString(result);
        response.getWriter().write(s);
        response.getWriter().flush();
    }


}
